package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class regdemo
{
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
//        Class.forName("com.mysql.cj.jdbc.Driver");
//        System.out.println("正在连接");
//        Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
//                "root",
//                "root");
//        System.out.println("连接成功");
//        Scanner sc=new Scanner(System.in);
//        System.out.println("请输入用户名");
//        String username=sc.next();
//        System.out.println("请输入密码");
//        String password=sc.next();
//        System.out.println("请输入昵称");
//        String nickname=sc.next();
//        System.out.println("请输入年龄");
//        int age=sc.nextInt();
//        Statement sa=con.createStatement();
//        String sql="INSERT INTO USER(USERNAME,PASSWORD,NICKNAME,AGE) VALUES('"+username+"','"+password+"','"+nickname+"',"+age+")";
//        int count=sa.executeUpdate(sql);
//        System.out.println(count>0?"成功":"失败");
//        sa.close();
//        /*user user=new user();
//        user.setUsername(username);
//        user.setPassword(password);
//        user.setNickname(nickname);
//        user.setAge(age);*/
        user user=InputUtil.getInputObject(new user(),"欢迎注册");
        insert(user);

    }
    public static  void insert (user user) throws ClassNotFoundException, SQLException
    {
        /*
        INSERT INTO USER(USERNAME,PASSWORD,NICKNAME,AGE) VALUES()
         */
            String sql="INSERT INTO USER(USERNAME,PASSWORD,NICKNAME,AGE) VALUES('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getNickname()+"',"+user.getAge()+")";
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("正在连接");
            Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root",
                    "root");
            Statement sa=con.createStatement();
            int count=sa.executeUpdate(sql);
            System.out.println(count>0?"成功":"失败");
            sa.close();
    }
}
